/*
 * @Author: Snow
 * @Date: 2021-10-11 11:37:07
 * @Description: 纹理工具类
 */
var ctx = document.createElement("canvas").getContext("2d");
export class TextrueUtil {
    public static setCanvasSize(width: number, height: number): void {
        ctx.canvas.width = width;
        ctx.canvas.height = height;
    };

    public static makeTexture(gl: WebGLRenderingContext): WebGLTexture {
        var tex = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, tex);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, ctx.canvas);
        gl.generateMipmap(gl.TEXTURE_2D);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        return tex;
    };

    public static makeStripeTexture(gl: WebGLRenderingContext, options: any): WebGLTexture {
        options = options || {};
        var width = options.width || 2;
        var height = options.height || 2;
        var color1 = options.color1 || "white";
        var color2 = options.color2 || "black";

        this.setCanvasSize(width, height);

        ctx.fillStyle = color1 || "white";
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = color2 || "black";
        ctx.fillRect(0, 0, width, height / 2);

        return this.makeTexture(gl);
    };

    public static makeCheckerTexture(gl: WebGLRenderingContext, options: any): WebGLTexture {
        options = options || {};
        var width = options.width || 2;
        var height = options.height || 2;
        var color1 = options.color1 || "white";
        var color2 = options.color2 || "black";

        this.setCanvasSize(width, height);

        ctx.fillStyle = color1 || "white";
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = color2 || "black";
        ctx.fillRect(0, 0, width / 2, height / 2);
        ctx.fillRect(width / 2, height / 2, width / 2, height / 2);

        return this.makeTexture(gl);
    };

    public static makeCircleTexture(gl: WebGLRenderingContext, options: any): WebGLTexture {
        options = options || {};
        var width = options.width || 128;
        var height = options.height || 128;
        var color1 = options.color1 || "white";
        var color2 = options.color2 || "black";

        this.setCanvasSize(width, height);

        var size = Math.min(width, height);
        ctx.fillStyle = color1 || "white";
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = color2 || "black";
        ctx.save();
        ctx.translate(width / 2, height / 2);
        ctx.beginPath();
        ctx.arc(0, 0, width / 2 - 1, 0, Math.PI * 2);
        ctx.fill();
        ctx.fillStyle = color1 || "white";
        ctx.beginPath();
        ctx.arc(0, 0, width / 4 - 1, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();

        return this.makeTexture(gl);
    };

    public static makeRandomTexture(gl: WebGLRenderingContext, options: any): WebGLTexture {
        options = options || {};

        let randInt = (min: number, max: number): number => {
            return (min + Math.random() * (max - min)) >> 0;
        }

        var w = options.width || 2;
        var h = options.height || 2;
        var min = options.min || 0;
        var max = options.max || 256;

        var numPixels = w * h;
        var pixels = new Uint8Array(numPixels * 4);
        for (var p = 0; p < numPixels; ++p) {
            var off = p * 4;
            pixels[off + 0] = randInt(min, max);
            pixels[off + 1] = randInt(min, max);
            pixels[off + 2] = randInt(min, max);
            pixels[off + 3] = 255;
        }
        var texture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
        gl.generateMipmap(gl.TEXTURE_2D);
        return texture;
    };
}